package 学生管理系统;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;


public class App {
    static ArrayList<User> list = new ArrayList<>();
    static {
        // 静态代码块 在程序开始的时候 自动进行的运行
        list.add(new User("zhangsan","123456","1305352000210192411","17325602002"));
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        while (true) {
            System.out.println("1.登录");
            System.out.println("2.注册");
            System.out.println("3.忘记密码");
            System.out.println("4.退出");
            System.out.println("请输入你的选择");

            int select = sc.nextInt();
            switch (select) {
                case 1 -> login(list);
                case 2 -> register(list);
                case 3 -> forgetPassword(list);
                case 4 -> System.exit(0);
            }
        }

    }

    public static void login(ArrayList<User> list) {

        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < 3; i++) {
            System.out.println("请输入用户名");
            String userName = sc.next();
            // 判断用户名是否存在
            if (!contains(list, userName)) {
                System.out.println("用户名未注册 请先注册");
                return;
            }

            System.out.println("请输入密码");
            String password = sc.next();

            // 生成验证码
            while (true) {
                String code = setCode();
                System.out.println("当前正确验证码为：" + code);
                System.out.println("请输入验证码");
                String userCode = sc.next();
                if (code.equals(userCode)) {
                    System.out.println("登录成功");
                    System.out.println("欢迎使用学生信息管理系统");
                    break;
                } else {
                    System.out.println("验证码输入错误");
                    continue;
                }
            }

            // 判断用户名和密码是否正确
            if (checkUserInfo(list, userName, password)) {
                //正确
                System.out.println("登录成功");
                Student_test s=new Student_test();
                s.startStudentsystem();
                break;
            } else {
                //错误
                System.out.println("用户名或密码错误");
                if (i == 2) {
                    System.out.println("您输入密码的次数已经到达上限 账号呗锁定 请联系客服：XXX-XXXX");
                    return;
                } else {
                    System.out.println("您还剩下" + (2 - i) + "次输入机会");
                    continue;
                }
            }

        }

    }

    private static boolean checkUserInfo(ArrayList<User> list, String userName, String password) {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getUsername().equals(userName) && list.get(i).getPassword().equals(password)) {
                return true;
            }
        }
        return false;
    }

    private static String setCode() {
        // 创建一个集合 里面有大小写字母
        ArrayList<Character> list = new ArrayList<>();
        for (int i = 0; i < 26; i++) {
            list.add((char) ('a' + i));
            list.add((char) ('A' + i));
        }
        //System.out.println(list);
        Random r = new Random();
        String result = "";

        for (int i = 0; i < 4; i++) {
            int randomIndex = r.nextInt(list.size());
            result += list.get(randomIndex);
        }
        //System.out.println(result);
        int randomNumber = r.nextInt(10);
        result += randomNumber;

        // 随机获取一个索引进行交换
        char[] arr = result.toCharArray();
        int randomindex2 = r.nextInt(arr.length);

        char temp = arr[randomindex2];
        arr[randomindex2] = arr[arr.length - 1];
        arr[arr.length - 1] = temp;

        //System.out.println(new String(arr));

        return new String(arr);
    }

    public static void register(ArrayList<User> list) {
        Scanner sc = new Scanner(System.in);
        // 校验username
        String username;
        while (true) {
            System.out.println("请输入用户名");
            username = sc.next();

            if (checkUsername(username)) {
                if (contains(list, username)) {
                    System.out.println("用户名已经存在 请重新输入");
                    continue;
                } else {
                    System.out.println("用户名输入成功");
                    break;
                }

            } else {
                System.out.println("用户名格式不符合规范 请重新输入");
            }
        }
        // 校验密码
        String password1;
        while (true) {
            System.out.println("请输入密码");
            password1 = sc.next();
            System.out.println("请再次输入密码");
            String password2 = sc.next();
            if (password1.equals(password2)) {
                System.out.println("密码设置成功");
                break;
            } else {
                System.out.println("密码有误请重新输入");
                continue;
            }
        }
        // 校验身份证号
        String personID;
        while (true) {
            System.out.println("请输入身份证号");
            personID = sc.next();
            if (checkPersonID(personID)) {
                System.out.println("身份证号输入成功");
                break;
            } else {
                System.out.println("身份证号输入格式不正确");
                continue;
            }
        }
        // 校验手机号
        String phoneNumber;
        while (true) {
            System.out.println("请输入手机号");
            phoneNumber = sc.next();
            if (checkPhontNumber(phoneNumber)) {
                System.out.println("手机号输入成功");
                break;
            } else {
                System.out.println("手机号输入格式不正确");
                continue;
            }
        }

        User u = new User(username, password1, personID, phoneNumber);
        list.add(u);
        printList(list);
    }

    private static void printList(ArrayList<User> list) {
        for (int i = 0; i < list.size(); i++) {
            System.out.println("用户名:" + list.get(i).getUsername() + " 手机号：" + list.get(i).getPhonenumber() + " 身份证号：" + list.get(i).getPersonid());
        }
    }

    private static boolean checkPhontNumber(String phoneNumber) {
        if (phoneNumber.length() != 11) {
            return false;
        }
        if (phoneNumber.startsWith("0")) {
            return false;
        }
        for (int i = 0; i < phoneNumber.length(); i++) {
            char c = phoneNumber.charAt(i);
            if (!(c >= '0' && c <= '9')) {
                return false;
            }
        }
        return true;
    }

    private static boolean checkPersonID(String personID) {
        if (personID.length() != 18) {
            return false;
        }
        if (personID.startsWith("0")) {
            return false;
        }
        for (int i = 0; i < personID.length() - 1; i++) {
            char c = personID.charAt(i);
            if (!(c >= '0' && c <= '9')) {
                return false;
            }
        }
        char end = personID.charAt(personID.length() - 1);
        if (!((end >= '0' && end <= '9') || (end == 'x') || (end == 'X'))) {
            return false;
        }
        return true;
    }

    private static boolean contains(ArrayList<User> list, String username) {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getUsername().equals(username)) {
                return true;
            }
        }
        return false;
    }

    private static boolean checkUsername(String username) {
        int len = username.length();
        // 先判断格式是否正确
        if (len < 3 || len > 15) {
            System.out.println("长度不符合要求");
            return false;
        }
        for (int i = 0; i < username.length(); i++) {
            char c = username.charAt(i);
            if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'))) {
                System.out.println("只能是字母和数字的组合");
                return false;
            }
        }
        int count = 0;
        for (int i = 0; i < username.length(); i++) {
            char c = username.charAt(i);
            if ((c > 'a' && c < 'z') || (c > 'A' && c < 'Z')) {
                count++;
            }
        }
        return count > 0;
    }

    public static void forgetPassword(ArrayList<User> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名");
        String username = sc.next();

        if (!contains(list, username)) {
            System.out.println("用户名不存在");
            return;
        }

        System.out.println("请输入身份证号");
        String personid = sc.next();

        System.out.println("请输入手机号码");
        String phoneNumber = sc.next();

        // 获取当前用户名的索引
        int index = findIndex(list, username);
        if (!(list.get(index).getPersonid().equalsIgnoreCase(personid) && list.get(index).getPhonenumber().equals(phoneNumber))) {
            System.out.println("账号信息不匹配 修改失败");
            return;
        }

        while (true) {
            System.out.println("请输入密码");
            String password = sc.next();
            System.out.println("请再次输入密码");
            String againpassword = sc.next();
            if (password.equals(againpassword)) {
                System.out.println("修改成功");
                list.get(index).setPassword(password);
                break;
            } else {
                System.out.println("两次输入密码不一致 请重新输入");
                continue;
            }
        }
    }

    private static int findIndex(ArrayList<User> list, String username) {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getUsername().equals(username)) {
                return i;
            }
        }
        return -1;
    }

}
